package com.bangdream.michelia.view.adapter;



import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.courses.CourseInfoBean;
import com.bangdream.michelia.utils.NumberToChnUtils;
import com.bangdream.michelia.utils.PublicUtil;
import com.bangdream.michelia.utils.TextViewUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

/**
 * Created by zhouyi on 2018/3/25.
 */

public class CourseChapterAdapter extends BaseQuickAdapter<CourseInfoBean.ChapterInfosBean,BaseViewHolder> {
    private int defItem = -1;
    private boolean isFinish = false;

    public CourseChapterAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, CourseInfoBean.ChapterInfosBean item) {
        int redColor = mContext.getResources().getColor(R.color.common_red);
        int blackColor = mContext.getResources().getColor(R.color.title);



        helper.setText(R.id.tvPosition,"第"+NumberToChnUtils.NumberToChn(helper.getAdapterPosition()+1)+"节");
        helper.setText(R.id.tvName,item.getName()+"");
        helper.setText(R.id.tvTime,"本章学习时间"+ PublicUtil.tranForm(item.getAllHour())+"分钟");

        String status = item.getStatus();
        TextView statusView = helper.getView(R.id.tvGoExamination);


        GradientDrawable gd =(GradientDrawable) statusView.getBackground();

//        是否学习（0 未学习，1 学习中，2已学完）
        switch (status){
            case "0":
                helper.setText(R.id.tvGoExamination,"未学");
                gd.setColor(redColor);
                statusView.setTextColor(Color.WHITE);
                statusView.setBackgroundDrawable(gd);
                break;
            case "1":
                helper.setText(R.id.tvGoExamination,"学习中");
                gd.setColor(redColor);
                statusView.setTextColor(Color.WHITE);
                statusView.setBackgroundDrawable(gd);
                break;
            case "2":
                helper.setText(R.id.tvGoExamination,"已学");
                gd.setColor(Color.WHITE);
                statusView.setTextColor(redColor);
                statusView.setBackgroundDrawable(gd);
                break;
        }

        if (defItem != -1) {
            if (defItem == helper.getAdapterPosition()) {
                helper.setTextColor(R.id.tvName,redColor);
                if(!item.getStatus().equals("2")){
                    helper.setText(R.id.tvGoExamination,"学习中");
                }
            } else {
                helper.setTextColor(R.id.tvName,blackColor);
            }
        }

        if(isFinish && defItem== helper.getAdapterPosition()){
            helper.setText(R.id.tvGoExamination,"已学");
            helper.setTextColor(R.id.tvName,redColor);
            gd.setColor(Color.WHITE);
            statusView.setTextColor(redColor);
            statusView.setBackgroundDrawable(gd);
        }
    }


    public void setDefSelect(int position) {
        this.defItem = position;
        notifyDataSetChanged();
    }

    public void setFinish(){
        this.isFinish=true;
        notifyDataSetChanged();
    }

}
